package cn.xiehang.SoundSight;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class RecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
    private List<RecyclerViewItem> mData;

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public ImageView imageView; // 假设你有一个ImageView在布局中
        public RecyclerViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view);
            imageView = itemView.findViewById(R.id.item_image); // 假设你有一个ImageView在布局中
        }
    }

    public RecyclerViewAdapter (List<RecyclerViewItem> data) {
        mData = data;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_third_recycler_item_list, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        RecyclerViewItem item = mData.get(position);

        // 1. 动态设置 Item 高度（示例固定值）
        ViewGroup.LayoutParams itemParams = holder.itemView.getLayoutParams();
        itemParams.height = dpToPx(300,holder); // 单位px（建议用dpToPx转换）
        holder.itemView.setLayoutParams(itemParams);

//        // 2. 仅调整奇数位（右边）图片的margin
//        ViewGroup.MarginLayoutParams imageParams = (ViewGroup.MarginLayoutParams)
//                holder.imageView.getLayoutParams();
////
//        if (position % 2 != 0) { // 右边图片
//            imageParams.topMargin = dpToPx(50,holder); // 下移150px（建议用dp）
//        } else { // 左边图片
//            imageParams.topMargin = 0; // 重置margin
//        }
//        holder.imageView.setLayoutParams(imageParams);

        // 3. 设置文本和图片
        holder.textView.setText(item.getTitle());
        Glide.with(holder.itemView.getContext())
                .load(item.getImageUrl())
                .into(holder.imageView);

        // 设置图片宽高（如果设置了的话）
        if (item.getWidth() > 0 && item.getHeight() > 0) {
            ViewGroup.LayoutParams params = holder.imageView.getLayoutParams();
            // 假设宽度是屏幕宽度的 80%，高度按比例计算
            int screenWidth = holder.itemView.getContext().getResources().getDisplayMetrics().widthPixels;
            params.width = (int) (screenWidth * 0.01*item.getWidth()); // 宽度占屏幕 80%
            params.height = (int) (params.width * 0.01*item.getHeight()); // 高度按比例缩放

            holder.imageView.setLayoutParams(params);
        }


    }
    private int dpToPx(int dp, RecyclerViewHolder holder) {
        return (int) (dp * holder.itemView.getContext().getResources().getDisplayMetrics().density);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}